<template>
  <div class="menu-wrapper">
    <!--    <company-dialog ref="officeDialog" @confirm="dialogConfirm" />-->
    <!--    <office-dialog ref="officeDialog" @confirm="officeConfirm"/>-->
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <div
        style="width: 100%;display: flex;align-items: flex-end;border-bottom: 1px #eee solid;margin-bottom: 20px"
      >
        <span style="font-size: 18px;color:#409EFF;margin: 10px 0;">新增客户经理</span>
      </div>
      <el-form-item label="客户经理名称:" prop="name">
        <el-col :span="6">
          <el-input v-model="form.name" placeholder="请输入客户经理名称"/>
        </el-col>
      </el-form-item>
      <el-form-item label="供货商:" prop="customerName">
        <el-col :span="6">
          <query-supplier  @querySupplier='querySupplierClick' :supplierId="form.customerId" :supplierName="form.customerName"/>
        </el-col>
      </el-form-item>
      <el-form-item label="联系方式:" prop="phone">
        <el-col :span="6">
          <el-input type="number" v-model="form.phone" clearable />
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="clicked" @click="update('form')">修 改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<style scoped>
.menu-wrapper {
  margin: 15px;
}
/deep/ .el-input-number--mini{
  width: 100%;
}
/deep/ .el-input__inner{
  text-align: left !important;
}
</style>
<script>
import {EventBus} from "@/api/eventBus";
import utils from "../../utils/utils.js";
import md5 from "js-md5";
// import Distpicker from "@/components/Distpicker/Distpicker";
import {update, find} from "@/api/manager";

import QuerySupplier from "@/components/Query/querySupplier";


export default {
  name: "customerManager-edit",

  components: {
    QuerySupplier,
  },
  data() {
    const phone = (rule, value, callback)=>{

      if (utils.isNull(value)) {
        callback(new Error('请输入手机号'));
      } else if (value.length < 11) {
        callback(new Error('请核对手机号'));
      } else if (!/^1(3|4|5|7|8)\d{9}$/.test(value)) {
        callback(new Error('请输入正确的手机号'));
      }else {
        callback();
      }

    }
    return {
      form:{
        id:null,
        name:'',
        customerName:'',
        customerId:'',
        phone:'',
        status:0
      },
      clicked:false,
      query:{
        customerId:'',
        customerName:'',
      },
      rules: {
        name: [
          { required: true, message: "请选择客户经理名称", trigger: "blur" },
          { min: 2, max: 20, message: "长度在 2 到 20 个字符", trigger: "blur" }
        ],
        phone: [
          { required: true, validator:phone, trigger: "blur", }
        ],

        customerName:[
          { required: true, message: "请输入详情地址", trigger: "blur"}
        ],
      },
    };
  },
  created() {
    var _this = this;
    _this.getData()
  },
  watch: { // 路由地址不变的情况下,如果id变化则重新获取数据
    $route(newVal, oldVal) {
      if (newVal.name == 'customerManager-edit' && newVal.query.id != this.id) {
        this.getData()
      }
    },
  },
  methods: {
    querySupplierClick(item){
      console.log("选择2",item)
      this.query.customerId = item.id
      this.query.customerName = item.name
      this.form.customerId = this.query.customerId
      this.form.customerName = this.query.customerName
      console.log("选择供应商",this.form.customerName,this.query.customerId)
    },
    getData() {
      this.form.id = this.$route.query.id;
      var _this = this;
      find(_this.form.id)
        .then(response => {
          console.log(response,'find')
          if (response.type == "success") {
            _this.form = response.data.manager;
            _this.form.customerId = response.data.customer.id;
            _this.form.customerName = response.data.customer.name;
          } else {
            _this.$message({
              message: response.content,
              offset: 120,
              center: true
            });

          }
        })
        .catch(error => {
        });
    },
    update(form) {
      const _this = this;
      console.log("form",this.$refs[form].model);
      // console.log("点击创建了", this.$refs[form],)
      this.$refs[form].validate(valid => {
        console.log("点击创建了", valid)
        if (valid) {
          // 校验表单
          var list = {
            id :_this.form.id ,
            name :_this.form.name ,
            customerId :_this.form.customerId ,
            customerName :_this.form.customerName ,
            phone :_this.form.phone,
            status :_this.form.status
          }
          _this.clicked = true;
          update(list)
            .then(response => {
              _this.clicked = false;
              if (response.type == "success") {
                let data = _this.formatList(response.data);
                console.log("修改完返回给列表的数据--" + JSON.stringify(data));
                EventBus.$emit("product-add", data);
                _this.$store
                  .dispatch("tagsView/delView", _this.$route)
                  .then(({visitedViews}) => {
                    _this.$router.push({
                      path: "/customerManager/list",
                      query: {id: _this.form.id, name: _this.form.name}
                    });
                  }); // 关闭当前tab回到列表
              } else {
                _this.$message({
                  message: response.content,
                  offset: 120,
                  center: true
                });
                console.log("content",response.content);

              }
            })
            .catch(error => {
              _this.clicked = false;
            });
        } else {
          return false;
        }
      });
    },
    //过滤成日期
    formatDate2Day(val) {
      let date = new Date(val);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      return y + "-" + MM + "-" + d;
    },

    //过滤到时间
    formatDate2Time(val) {
      let date = new Date(val);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      let h = date.getHours();
      h = h < 10 ? "0" + h : h;
      let m = date.getMinutes();
      m = m < 10 ? "0" + m : m;
      let s = date.getSeconds();
      s = s < 10 ? "0" + s : s;
      return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
    },

    //过滤企业列表
    formatList(data) {
      data.createDate = this.formatDate2Day(data.createDate);
      data.modifyDate = this.formatDate2Time(data.modifyDate);
      data.rechangeDate = this.formatDate2Time(data.rechangeDate);
      return data;
    },
  }
};
</script>
